<?php

class Application_Model_Pegawai extends Zend_Db_Table_Abstract
{
    protected $_name = 'tabel_pegawai';
    
    public function pegawaiBaru($data)
    {
        $this->insert($data);
    }
    
    public function getPegawai($id) {
        $id = (int)$id;
        $row = $this->fetchRow('id_pegawai = '.$id);
        if(!$row)
            return false;
        else
            return $row->toArray ();
    }
    
    public function getPegawaiNik($nik) {
        $row = $this->fetchRow($this->select()->where('nik = ?', $nik));
        
        if(!$row)
            return false;
        else
            return $row->toArray();
    }
    
    public function getPegawaiKosong($nik) {
        $row = $this->fetchRow($this->select()->where('nik = ?', $nik)->where('status_pekerjaan != 1'));
        
        if(!$row)
            return false;
        else
            return $row->toArray();
    }
    
    public function getPegawaiKos() {
        $row = $this->fetchAll($this->select()->where('status_pekerjaan != 1'));
        
        if(!$row)
            return false;
        else
            return $row->toArray();
    }
    
    public function pegawaiUpdate($data, $id)
    {
        $this->update($data, 'id_pegawai = '.(int)$id);   
    }
    
    public function pegawaiDelete($id){
        $this->delete('id_pegawai = '.(int)$id);
    }


    public function checkpegawai($nama) {
        $select = $this->select();
        $select->where("nama_pegawai= ?",$nama);
        $row = $this->fetchAll($select)->count();
        if($row > 0)
            return true;
        else
            return false;
    }
    
    public function  getAllData()
    {
        $data = $this->fetchAll()->toArray();
        return $data;
    }
    
    public function getAllPegawaiKosong()
    {
        $select = $this->select();
        $select->from($this->_name)
               ->where('status_pekerjaan != ?', 1);
        
        $result = $this->fetchAll($select);
        return $result->toArray();
    }
    
    public function checkRowPegawai()
    {
        $row = $this->fetchAll()->count();
        return $row;
    }
    
    public function getDataJoin()
    {
        $select = $this->select();
        $select->setIntegrityCheck(false);
        $select->from($this->_name);
        
        $select->joinLeft('tabel_departemen', 'tabel_pegawai.id_departemen = tabel_departemen.id_departemen', 
                            array('nama_departemen' => 'nama_departemen'))
               ->joinLeft('tabel_posisi', 'tabel_pegawai.id_posisi = tabel_posisi.id_posisi', 
                            array('nama_posisi' => 'nama_posisi'))
                ->joinLeft('tabel_status_pegawai', 'tabel_pegawai.id_status_pegawai = tabel_status_pegawai.id_status_pegawai',
                            array('status_pegawai' => 'status_pegawai'));
    
        $result = $this->fetchAll($select)->toArray();
        return $result;
    }
    
    public function getDataJoinWithId($id)
    {
        $select = $this->select();
        $select->setIntegrityCheck(false);
        $select->from($this->_name)
               ->where('id_pegawai = ?', $id);
        
        $select->joinLeft('tabel_departemen', 'tabel_pegawai.id_departemen = tabel_departemen.id_departemen', 
                            array('nama_departemen' => 'nama_departemen'))
               ->joinLeft('tabel_posisi', 'tabel_pegawai.id_posisi = tabel_posisi.id_posisi', 
                            array('nama_posisi' => 'nama_posisi'))
                ->joinLeft('tabel_status_pegawai', 'tabel_pegawai.id_status_pegawai = tabel_status_pegawai.id_status_pegawai',
                            array('status_pegawai' => 'status_pegawai'));
    
        $result = $this->fetchAll($select)->toArray();
        return $result;
    }
    
    public function getAllNik()
    {
        $select = $this->select()->from($this->_name, 'nik');
        $result = $this->fetchAll($select)->toArray();
        return $result;
    }
    
    public function getUser($id)
    {
        $select = $this->select();
        $select->setIntegrityCheck(false)
               ->from($this->_name)
               ->where('id_pegawai = ?', $id)
               ->joinLeft('tabel_user', 'tabel_pegawai.id_pegawai = tabel_user.id_pegawai');
        
        $result = $this->fetchAll($select)->toArray();
        return $result;
    }
}

